package com.cyj.dream.core.util.pojo;

import com.cyj.dream.core.constant.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Description: 用于全局异常拦截返回结果实体
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.core.util.pojo
 * @Author: ChenYongJia
 * @CreateTime: 2021-09-26 10:55
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Data
@AllArgsConstructor // 自动所有参数的构造方法方法
@NoArgsConstructor // 自动无参的构造方法方法
@ApiModel(value = "ApiResult对象", description = "结果实体类")
public class ApiResult implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "返回状态码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Object data;

    public ApiResult(ResultCode resultCode, Object data) {
        this.code = resultCode.code();
        this.message = resultCode.message();
        this.data = data;
    }

    private void setResultCode(ResultCode resultCode) {
        this.code = resultCode.code();
        this.message = resultCode.message();
    }

    /**
     * 返回成功
     *
     * @return
     */
    public static ApiResult success() {
        ApiResult result = new ApiResult();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 返回成功
     *
     * @param data
     * @return
     */
    public static ApiResult successAndData(Object data) {
        ApiResult result = new ApiResult();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 返回失败
     *
     * @param code
     * @param message
     * @return
     */
    public static ApiResult failAndMessage(Integer code, String message) {
        ApiResult result = new ApiResult();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 返回失败
     *
     * @param resultCode
     * @return
     */
    public static ApiResult fail(ResultCode resultCode) {
        ApiResult result = new ApiResult();
        result.setResultCode(resultCode);
        return result;
    }

}
